package com.hsnn.medstgmini.dao.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.CommonDao;
import com.hsnn.medstgmini.sys.model.SysSequence;

@Repository
@Scope("singleton")
public class CommonDaoImpl implements CommonDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * @category 获取各机构帐号序列
	 * @date 2015年10月16日
	 * @param userType
	 * @return
	 */
	public Long getSequence(int userType) {
		Long sequence = null;
		synchronized (SysSequence.class) {
			Session session = sessionFactory.getCurrentSession();
			Criteria criteria = session.createCriteria(SysSequence.class);
			criteria.add(Restrictions.eq("id", String.valueOf(userType)));
			SysSequence ss = (SysSequence) criteria.uniqueResult();
			if (ss != null) {
				String seq = ss.getSequence();
				if (StringUtils.isNotBlank(seq)) {
					sequence = Long.parseLong(seq);
					sequence = sequence + 1;
					ss.setSequence(sequence.toString());
					session.saveOrUpdate(ss);
					session.flush();
				}
			}
		}
		return sequence;
	}

	/**
	 * @category 获取各机构子帐号序列
	 * @date 2015年10月16日
	 * @param userType
	 * @return
	 */
	public Long getSubSequence(String mainAccount) {
		Long sequence = 100L;
		synchronized (SysSequence.class) {
			Session session = sessionFactory.getCurrentSession();
			Criteria criteria = session.createCriteria(SysSequence.class);
			criteria.add(Restrictions.eq("id", mainAccount));
			SysSequence ss = (SysSequence) criteria.uniqueResult();
			if (ss == null) {
				ss = new SysSequence();
				ss.setId(mainAccount);
				ss.setSequence(String.valueOf(sequence));
				session.saveOrUpdate(ss);
				session.flush();
			} else {
				String seq = ss.getSequence();
				if (StringUtils.isNotBlank(seq)) {
					sequence = Long.parseLong(seq);
					sequence = sequence + 1;
					ss.setSequence(sequence.toString());
					session.saveOrUpdate(ss);
					session.flush();
				}
			}
		}
		return sequence;
	}
	
	public Long getSequence(String code) {
		Long sequence = 100L;
		synchronized (SysSequence.class) {
			Session session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(SysSequence.class);
			criteria.add(Restrictions.eq("id", code));
			SysSequence ss = (SysSequence) criteria.uniqueResult();
			if (ss == null) {
				ss = new SysSequence();
				ss.setId(code);
				ss.setSequence(String.valueOf(sequence));
				session.saveOrUpdate(ss);
				session.flush();
			} else {
				String seq = ss.getSequence();
				if (StringUtils.isNotBlank(seq)) {
					sequence = Long.parseLong(seq);
					sequence = sequence + 1;
					ss.setSequence(sequence.toString());
					session.saveOrUpdate(ss);
					session.flush();
				}
			}
			session.clear();
			session.close();
		}
		
		return sequence;
	}

}
